package com.smartcar.sms.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.smartcar.common.domain.request.SendEmailVo;
import com.smartcar.common.result.PageResult;
import com.smartcar.common.result.Result;
import com.smartcar.sms.service.SmsService;
import com.sms.common.domain.SmsTemplates;
import com.sms.common.domain.request.QuerySmsVo;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName 小渣渣
 * @Description TODO
 * @date 2023/10/26 16:33
 * @Version 1.0
 */

/**
 * 短信模板管理
 */
@RestController
@RequestMapping("sms")
@Log4j2
public class SmsController {

    @Autowired
    private SmsService smsService;

    @Autowired
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String username;

    /**
     * 条件查询短信模板
     */
    @PostMapping("querySms")
    public Result<PageResult<SmsTemplates>> querySms(@RequestBody QuerySmsVo querySmsVo) {
        log.info("功能名称：条件查询短信模板，入参：{}", JSONObject.toJSONString(querySmsVo));
        PageInfo<SmsTemplates> info = smsService.querySms(querySmsVo);
        Result<PageResult<SmsTemplates>> result = Result.success(PageResult.toPageResult(info.getTotal(), info.getList()));
        log.info("方法名称：条件查询短信模板，出参：{}", JSONObject.toJSONString(result));
        return result;
    }

    /**
     * 添加短信模板
     */
    @PostMapping("insertSms")
    public Result insertSms(@RequestBody SmsTemplates smsTemplates) {
        log.info("功能名称：添加短信模板，入参：{}", JSONObject.toJSONString(smsTemplates));
        int i = smsService.insertSms(smsTemplates);
        Result result = null;
        if (i > 0) {
            result = Result.success(i);
        } else {
            result = Result.error(i);
        }
        log.info("功能名称：添加短信模板，出参：{}", JSONObject.toJSONString(result));
        return result;
    }

    /**
     * 修改短信模板
     */
    @PostMapping("updateSmsById")
    public Result updateSms(@RequestBody SmsTemplates smsTemplates) {
        log.info("功能名称：修改短信模板，入参：{}", JSONObject.toJSONString(smsTemplates));
        int i = smsService.updateSmsById(smsTemplates);
        Result result = null;
        if (i > 0) {
            result = Result.success(i);
        } else {
            result = Result.error(i);
        }
        log.info("功能名称：修改短信模板，出参：{}", JSONObject.toJSONString(result));
        return result;
    }

    /**
     * 发送邮箱
     */
    @PostMapping("sendEmail")
    public Result sendEmail(@RequestBody SendEmailVo sendEmailVo) {
        smsService.sendEmail(sendEmailVo);
        return Result.success();
    }

    /**
     * 发送登录成功邮箱
     */
    @PostMapping("loginEmail")
    public Result loginEmail(@RequestParam("nickName") String nickName, @RequestParam("email") String email) {
        smsService.loginEmail(nickName, email);
        return Result.success();
    }
}
